import React from 'react';
import { inject, observer } from 'mobx-react';
import { toJS } from "mobx"
import { WeaSearchGroup, WeaBrowser, WeaUpload, WeaLocaleProvider } from "ecCom"
import { Button, Row, Col, Spin, message } from 'antd';
const getLabel = WeaLocaleProvider.getLabel;

@inject('importStore')
@observer
class GovernImport extends React.Component {
    constructor(props) {
        super(props);
        this.state={
        }
    }
    
    getCircle() {
        let style = {
            width: 20,
            height: 20,
            backgroundColor: '#D8D8D8',
            webkitBorderRadius: 10,
            mozBorderRadius: 10,
            msBorderRadius: 10,
            oBorderRadius: 10,
            borderRadius: 10,
            textAlign: 'center',
        }
        return style;
    }

    getForm() {

        const { importStore } = this.props;
        const {mandstr,otherstr,importmsg} = importStore;
        const require = "<span>" + getLabel(18019, "必填") + ": " + mandstr + " </span>";
        const other = "<span>" + getLabel(375, "其他") + ":  " + otherstr + " </span>";
        let condition =
            [{
                "title": getLabel(24893, "基础信息"),
                "defaultshow": true,
                "items": [
                    {
                    "checkbox": false,
                    "colSpan": 2,
                    "conditionType": "BROWSER",
                    "detailtype": 1,
                    "domkey": ["categoryId"],
                    "fieldcol": 18,
                    "hasBorder": false,
                    "helpfulTipProps": {},
                    "isQuickSearch": false,
                    "label": getLabel(388808, "督办类型"),
                    "labelcol": 6,
                    "length": 0,
                    "precision": 0,
                    "showOrder": 0,
                    "stringLength": 0,
                    "tipPosition": "bottom",
                    "viewAttr": 2,
                    "value": ""
                },
                 {
                    "checkbox": false,
                    "colSpan": 2,
                    "conditionType": "RESOURCEIMG",
                    "detailtype": 1,
                    "domkey": ["excelfile"],
                    "fieldcol": 18,
                    "hasBorder": false,
                    "helpfulTipProps": {},
                    "isQuickSearch": false,
                    "label": getLabel(16699, "Excel文件"),
                    "labelcol": 6,
                    "length": 0,
                    "precision": 0,
                    "showOrder": 0,
                    "stringLength": 0,
                    "tipPosition": "bottom",
                    "viewAttr": 2
                }]
            }, {
                "title": getLabel(19010, "操作说明"),
                "defaultshow": true,
                "items": [{
                    "index": 0,
                    "value": getLabel(505309, "第一步，请选择督办类型，重生成模板")
                },{
                    "index": 1,
                    "value": getLabel(128226, "第二步，")
                }, {
                    "index": 2,
                    "value": getLabel(505311, "第三步，下载后，填写内容，注意，要填写的内容在下边的说明中有详细的说明，请一定要确定你的Excel文档的格式是模板中的格式，而没有被修改掉")
                }, {
                    "index": 3,
                    "value": getLabel(505312, "第四步，选择填写好的Excel文档，点击提交按钮进行批量导入")
                }, {
                    "index": 4,
                    "value": getLabel(505313, "第五步，如果以上步骤和Excel文档正确的话，数据会被正确的导入，导入成功会有提示。如果有问题，则会提示Excel文档的错误之处")
                }]
            }, {
                "title": getLabel(24962, "字段说明"),
                "defaultshow": true,
                "items": [{
                    "index": 1,
                    "value": getLabel(18617, "请按下列顺序排列需导入的EXCEL文档中各字段的顺序，其中红色的部分为必填字段，不能为空！")
                }, {
                    "index": 2,
                    "value": require
                }, {
                    "index": 3,
                    "value": other
                }]
            }]
        let _arr = [];

        condition.map((c, i) => {
            let arr = [];
            if (i == 0) { //基本信息
                c.items.map((field, index) => {
                    arr.push({
                        com: (
                            <div>
                                <Row ecId={`${this && this.props && this.props.ecId || ''}_Row@q9ca03@${field.domkey[0]}`}>
                                    <Col ecId={`${this && this.props && this.props.ecId || ''}_Col@flsn9s@${field.domkey[0]}`} span={1} offset={4}>
                                        <div style={{ marginTop: 15 }}>
                                            <div style={this.getCircle()}><div style={{ paddingTop: 2 }}>{index + 1}</div></div>
                                        </div>
                                    </Col>
                                    <Col ecId={`${this && this.props && this.props.ecId || ''}_Col@xhdzvn@${field.domkey[0]}`} span={14}>
                                        <div style={{ marginTop: 15 }}>
                                            <Row ecId={`${this && this.props && this.props.ecId || ''}_Row@o11i0x@${field.domkey[0]}`}>
                                                <Col ecId={`${this && this.props && this.props.ecId || ''}_Col@iihwmh@${field.domkey[0]}`} span={field.labelcol}>{field.label}:</Col>
                                                <Col ecId={`${this && this.props && this.props.ecId || ''}_Col@t77vlp@${field.domkey[0]}`} span={field.fieldcol}>
                                                    {this.getDom(field)}
                                                </Col>
                                            </Row>
                                        </div>
                                    </Col>
                                </Row>
                            </div>
                        ),
                        colSpan: 1,
                    })
                })
                //导入提示信息
                arr.push({
                    com: (
                        <div>
                            <Row ecId={`${this && this.props && this.props.ecId || ''}_Row@3fyg4u@xk${i}`}>
                                <Col ecId={`${this && this.props && this.props.ecId || ''}_Col@68xlo3@xk${i}`} offset={4}>
                                    <div dangerouslySetInnerHTML={{ __html: "<font size='2' color='#FF0000'>" + importmsg + "</font>" }}></div>
                                </Col>
                            </Row>
                        </div>
                    ),
                    colSpan: 1,
                })
            }
            if (i == 1) { //导入说明
                c.items.map((field, index) => {
                    arr.push({
                        com: (
                            <div>
                                <Row ecId={`${this && this.props && this.props.ecId || ''}_Row@4pr5z3@${index}`}>
                                    <Col ecId={`${this && this.props && this.props.ecId || ''}_Col@fy6aso@${index}`} span={1} offset={4} style={{ marginTop: 15 }}>
                                        <div style={this.getCircle()}>
                                            <div style={{ paddingTop: 2 }}>{index + 1}</div>
                                        </div>
                                    </Col>
                                    <Col ecId={`${this && this.props && this.props.ecId || ''}_Col@qw5ipt@${index}`} span={17} style={{ marginTop: 15 }}>
                                        {this.getIllustration(field.value, field.link, index, field)}
                                    </Col>
                                </Row>
                            </div>
                        ),
                        colSpan: 1
                    })
                })
            }
            if (i == 2) { //字段说明
                c.items.map((field, index) => {
                    arr.push({
                        com: (
                            <div>
                                <Row ecId={`${this && this.props && this.props.ecId || ''}_Row@sa02jq@${index}`}>
                                    <Col ecId={`${this && this.props && this.props.ecId || ''}_Col@y07r3p@${index}`} span={1} offset={4} style={{ marginTop: 15 }}>
                                        <div style={this.getCircle()}>
                                            <div style={{ paddingTop: 2 }}>{index + 1}</div>
                                        </div>
                                    </Col>
                                    <Col ecId={`${this && this.props && this.props.ecId || ''}_Col@a4d6tv@${index}`} span={17} style={{ marginTop: 15 }}>
                                        {this.getIllustration1(field.value, field.link, index, field)}
                                    </Col>
                                </Row>
                            </div>
                        ),
                        colSpan: 1
                    })
                })
            }
            _arr.push(<div><WeaSearchGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@j9uy3c@xk${i}`} needTigger={true} title={c.title} showGroup={c.defaultshow} items={arr} col={1} /></div>)
        });
        return _arr;
    }

    getDom(field) {
        const { importStore } = this.props;
        const { filelist, replaceDatas} = importStore;
        let dom;
        let domkey = field.domkey;
        if (domkey && domkey[0] == 'excelfile') {
            dom = (<div>
                <div style={{ float: 'left', marginTop: -10 }}>
                    <WeaUpload ecId={`${this && this.props && this.props.ecId || ''}_WeaUpload@whrvkh`}
                        uploadUrl='/api/doc/upload/uploadFile'
                        category='string'
                        limitType='xls'
                        maxFilesNumber={1}
                        datas={filelist}
                        onChange={(ids, list) => {
                            this.setFileId(ids, list)
                        }
                        } >
                        <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@o6zsje`}>{getLabel(125333, "选择文件")}</Button>
                    </WeaUpload>
                </div>
                <div style={{ float: 'left', paddingLeft: 10 }}>
                    {filelist.length == 0 ? <p>{getLabel(384040, "未选择任何文件")}</p> : filelist.map(file => <p>{file.filename}</p>)}
                </div>
            </div>)
        } else {
            dom = <div>
                <span style={{ display: "inline-block", float: "left", width: "35%", marginRight: 10 }}>
                    <WeaBrowser ecId={`${this && this.props && this.props.ecId || ''}_WeaBrowser@faevx2`} style={{ display: "inline" }} replaceDatas={toJS(replaceDatas)} type={'govCategory'} hasAdvanceSerach={true} title={getLabel(388808, "督办类型")}
                        onChange={(ids, names, datas) => {
                            importStore.saveCategory(ids, datas);
                        }} />
                </span>
                <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@e09zzk`} style={{ display: "inline", marginBottom: "5px" }} type="primary" onClick={() => this.regenTemplate()}>{getLabel(83555, "重生成模板")}</Button>
            </div>
        }
        return dom;
    }


    getIllustration(val, url, index, link) {
        const { importStore } = this.props;
        const { excelId,path } = importStore;
        let tempurl = "/weaver/weaver.file.ExcelOut?excelfile="+excelId;
        if(path !=""){
            tempurl = "/weaver/weaver.file.ExcelOut?path="+path; 
        }
        let p;
        let href = <a href={tempurl} >{getLabel(28446, "下载EXCEL文档模板")}</a>;
        if(excelId == 'ExcelFile_govern_0_'){
            href = <a href="javascript:;" onClick={() =>{ message.warn(getLabel('512494',"请生成模板后再下载！"));}}>{getLabel(28446, "下载EXCEL文档模板")}</a>
        }
        if (index == 1) {
            p = (<p>{val}{href}</p>);
        } else {
            p = (<p><div dangerouslySetInnerHTML={{ __html: val }} /></p>);
        }
        return p;
    }
 
    getIllustration1(val, url, index, link) {
        let p;
        if (index == 0) {
            p = (<p><div style={{ fontWeight: 700 }} dangerouslySetInnerHTML={{ __html: val }} /></p>);
        } else if (index == 1) {
            p = (<p><div style={{ color: "red" }} dangerouslySetInnerHTML={{ __html: val }} /></p>);
        } else {
            p = (<p><div dangerouslySetInnerHTML={{ __html: val }} /></p>);
        }
        return p;
    }

    render() {
        const { importStore } = this.props;
        const { spinning } = importStore;
        return (
            <div>
                <Spin ecId={`${this && this.props && this.props.ecId || ''}_Spin@9k9n7q`} spinning={spinning}>
                    {this.getForm()}
                </Spin>
            </div>
        )
    }
    setFileId(ids, list) {
        const { importStore } = this.props;
        importStore.excelfile = ids;
        importStore.filelist = list;
        importStore.importmsg="";
    }

    regenTemplate = (key) => {
        const { importStore } = this.props;
        importStore.regenTemplate();
    }

}
export default GovernImport;